ggplot2(10)-图例

基础绘图

1
2
3
library(ggplot2)
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group)) + geom_boxplot()
p

mark

移除图例

方法1

通过guides(fill=FALSE)可以移除图例

1
p + guides(fill=FALSE)

mark

方法2

通过设计scale_fill_discrete(guide=FALSE)来移除图例

1
p + scale_fill_discrete(guide=FALSE)

mark

方法3

通过主题来移除图例

1
p + theme(legend.position="none")

mark

修改图例的位置

图例的位置由theme(legend.position=…)决定,参数有top、left、right或bottom

置顶

1
2
3
4
5
6
7
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group)) +
geom_boxplot()+
scale_fill_brewer(palette="Paste12")
## Warning in pal_name(palette, type): Unknown palette Paste12
p + theme(legend.position="top")

mark

图例的精确位置

图例的精确位置可以通过legend.justification来调节,下面的代码表示将图例的右下角(1,0)置于绘图区域的右下角(1,0) 注:图例的是个矩形,原点是(0,0),右上角是(1,1)。绘图区域也是个矩形,原点是(0,0),右上角是(1,1)。这两个坐标体系表示的只是相对位置,并不是实际位置。

1
p + theme(legend.position=c(1,0),legend.justification=c(1,0))

mark

下面的代码表示将图例的右上角(1,1)置于绘图区域的右上角(1,1)

1
p + theme(legend.position=c(1,1),legend.justification=c(1,1))

mark

对图例进行修改

修改图例的边框

下面的代码表示,将图例放到绘图区域的(0.85,0.2)这个位置(图例的中心与这个点对齐),然后用白色填充图例(fill=“white”),边框为黑色(colour=“black”)

1
2
p + theme(legend.position=c(0.85,0.2)) +
theme(legend.background=element_rect(fill="white",colour="black"))

mark

去除图例的边框

1
2
3
p + theme(legend.position=c(0.85,0.2))+ # 图例的位置
theme(legend.background=element_blank())+ # 去掉图例的背景色
theme(legend.key=element_blank()) # 去掉每个图例项目周围的边框

mark

修改图例项目的顺序

初始绘图,图例中的顺序是ctrl,trt1,trt2。

1
2
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group)) + geom_boxplot()
p

mark

修改项目顺序

1
p + scale_fill_discrete(limits=c("trt1","trt2","ctrl"))

mark

修改的方式与修改x轴上的顺序一样:

1
2
3
p +
scale_fill_discrete(limits=c("trt1","trt2","ctrl"))+
scale_x_discrete(limits=c("trt1","trt2","ctrl"))

mark

更改颜色

可以对变量group进行颜色映射,如下所示:

1
p + scale_fill_grey(start=0.5,end=1,limits=c("trt1","trt2","ctrl"))

mark

也可以调用scale_fill_brewer()来进行颜色映射:

1
2
library(RColorBrewer)
p + scale_fill_brewer(palette="Set1",limits=c("trt1","trt2","ctrl"))

mark

反转图例项目的顺序

1
2
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
p

mark

反转图例

1
p + guides(fill=guide_legend(reverse=TRUE))

mark

反转图例的补充方法

1
p + scale_fill_hue(guide=guide_legend(reverse=TRUE))

mark

修改图例标题

方法1

labs(fill=“图例标题”)

1
2
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group)) + geom_boxplot()
p + labs(fill="Condition")

mark

方法2

用scale_fill_discrete(name=“Condition”)

1
p + scale_fill_discrete(name="Condition")

mark

修改图例标题的外观

1
2
3
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()
p + theme(legend.title=element_text(face="italic",family="Times",colour="red",size=14))

mark

上述代码运行后有警告:Windows字体数据库里没有这样的字体系列。R语言不能直接调用windows的字体,需要一个函数进行转换,例如我要使用Verdana这个字体,需要用到函数windowsFont,如下所示:

1
2
windowsFonts(Ver1=windowsFont("Verdana"))
p + theme(legend.title=element_text(face="italic",family="Ver1",colour="red",size=14))

mark

移除图例标题

原始图形

1
ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()

mark

现在移除图例上面的标题“group”,guides(fill=guide_legend(title=NULL),如下所示:

1
2
ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()+
guides(fill=guide_legend(title=NULL))

mark

修改图例标签

1
2
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group)) + geom_boxplot()
p + scale_fill_discrete(labels=c("Control","Treatment1","Treatment2"))

mark

上述图形中的图例标签进行了更改,由原来的Ctrl,trt1,trt2更改为了Control,Treatment1,Treatment2,但x轴上的类型数据标签还未更改,仍旧为Ctrl,trt1,trt2。现在对x轴的标签进行更改。

1
2
3
p +
scale_fill_discrete(labels=c("Control","Treatment1","Treatment2"))+
scale_x_discrete(labels=c("Control","Treatment1","Treatment2"))

mark

将数据类型映射到不同的灰度上:

1
p + scale_fill_grey(start=0.5,end=1,labels=c("Control","Treatment1","Treatment2"))

mark

如果修改了图例的顺序,则标签也会与之匹配:

1
2
p + scale_fill_discrete(limits=c("trt1","trt2","ctrl"),
labels=c("Treatment1","Treatment2","Control"))

mark

多个变量的图例

下面的例子中包含两个图例,即性别(sex)用颜色来表示,体重用点的大小来表示(weightLb)

1
2
3
4
5
6
7
8
library(gcookbook)
## Warning: package 'gcookbook' was built under R version 3.4.4
hw <- ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+
geom_point(aes(size=weightLb))+
scale_size_continuous(range=c(1,4))
hw

mark

添加图例的标题: 上面的例子中,有两个图例,因此可以对这两个图例分别设置标题:

1
hw + labs(colour="Male/Female",size="Weight\n(pounds)")

mark

如果一个变量被分别映射到两个图形属性上,则会默认生成一个组合了两种情况的图例:

1
2
3
hw1 <- ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,colour=sex))+
geom_point()
hw1

mark

修改上述绘图的标题,如果只修改一个,就会得到两个分离的图例:

1
hw1 + labs(shape="Male/Female")

mark

对颜色进行修改,得到的图例相同:

1
hw1 + labs(colour="Male/Female")

mark

同时修改shape与colour的标题:

1
hw1 + labs(shape="Male/Female",colour="Male/Female")

mark

有两个图形属性的标签

原始图形

1
2
3
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn,shape=sex,colour=sex)) +
geom_point()
p

mark

现在对上述图形进行修改,修改其中一个标度的标签:

1
p + scale_shape_discrete(labels=c("Female","Male"))

mark

同时修改两个标度:

1
2
p + scale_shape_discrete(labels=c("Female","Male"))+
scale_colour_discrete(labels=c("Female","Male"))

mark

修改图例标签外观

1
2
3
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group)) + geom_boxplot()
p + theme(legend.text=element_text(face="italic",family="Ver1",colour="red",size=14))

mark

通过guides来修改

1
p + guides(fill=guide_legend(label.theme=element_text(face="italic",family="Ver1",colour="red",size=14,angle=0.01)))

mark

使用多行文本的标签

1
2
p <- ggplot(PlantGrowth,aes(x=group,y=weight,fill=group)) + geom_boxplot()
p + scale_fill_discrete(labels=c("Control","Type 1\ntreatment","Type 2\ntreatment"))

mark

上面的图形的图例中文字间隔比较近,可以进行微调,其中,legend.text是图例中项目文字的调试,而legend.key.height则是项目符号的高度,调节效果如下所示:

1
2
3
4
library(grid)
p + scale_fill_discrete(labels=c("Control","Type 1\ntreatment","Type 2\ntreatment"))+
theme(legend.text=element_text(lineheight=0.9),
legend.key.height=unit(1,"cm"))

mark

参考资料

  1. 常肖楠, 邓一硕, 魏太云. R数据可视化手册[M]. 人民邮电出版社, 2014.